<template>
  <el-dialog
    :close-on-click-modal="false"
    title="文件夹编辑"
    :visible.sync="show"
    width="700px"
    center
  >
    <div class="btns">
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='same'">同级新增</el-button>
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='under'">下级新增</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="showAdd = true;addType=''">修改</el-button>
    </div>
    <div class="dirList">
      <tree-box v-model="selectedNode" :data="treeData" :no-search="true" title="目录" />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="show = false">确 定</el-button>
    </span>
    <el-dialog
      title="新增文件夹"
      :visible.sync="showAdd"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
        <el-form-item label="文件夹名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAdd = false">取 消</el-button>
        <el-button type="primary" @click="saveDir">确 定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { addMineDocumentDirectory, updateMineDocumentDirectory } from '@/api/knowledgeBase'
export default {
  name: 'DirManage',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入文件夹名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  methods: {
    saveDir() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const { id, parentId } = this.selectedNode
          const createId = this.addType === 'same' ? (parentId || null) : id
          const params = {
            ...this.form,
            parentId: this.addType ? createId : parentId,
            ...!this.addType ? { id } : {}
          }
          const fn = this.addType ? addMineDocumentDirectory : updateMineDocumentDirectory
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.showAdd = false
              this.$emit('freshTree')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dirList{
  max-height: 500px;
  min-height: 200px;
}

</style>
